package com.s64.modules;

import com.s64.entity.Order;
import com.s64.service.BookService;
import com.s64.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OrderConsumer {
    @Autowired
    private BookService bookService;
    @Autowired
    private OrderService orderService;

    @RabbitListener(queues = "order")
    public void createOrder(Order order) {
        System.out.println("异步创建订单:" + order.getOrderId());
        // 1. 修改秒杀库存
        bookService.setAmount(order.getBookId(), -1);
        // 2. 生成订单
        orderService.save(order);
    }
}
